#include <iostream>
#include <cstdio>
#include <vector>
#include <climits>
using namespace std;
typedef struct
{
	int link;
	int cost;
}arc;
vector<arc> a[30];
int n;
int dijkstra()
{
    int i,j,sum=0,min,pos,count=0;
    bool done[30];
  //  fill(done,done+30,0);
    done[0]=1;
    while(1)
    {
        for(i=0,pos=-1,min=INT_MAX;i<n;i++)
        {
            if(!done[i])
            {
                for(j=0;j<a[i].size();j++)
                {
                    if(done[a[i][j].link]&&a[i][j].cost<min)
                    {
                        pos=i;
                        min=a[i][j].cost;
                    }
                }
            }
        }
      //  cout<<"pos "<<pos<<"  min "<<min<<end;
        if(pos==-1) return sum;
        done[pos]=1;
        sum+=min;
    }
}
int main()
{
	int i,j,tn,tt,vi;
	char c;
	arc t;
	while(cin>>n)
	{
		if(n==0) return 0;
		for(i=0;i<30;i++) a[i].clear();
		for(i=0;i<n-1;i++)
		{
			cin>>c>>tn;
			vi=c-'A';
			//cout<<"vi"<<vi<<endl;
			for(j=0;j<tn;j++)
			{
				cin>>c>>t.cost;
				t.link=c-'A';
				//cout<<"t link "<<t.link<<endl;
				a[vi].push_back(t);
				tt=t.link;
				t.link=vi;
				a[tt].push_back(t);
			}
		}
		cout<<dijkstra()<<endl;
	}
	return 0;
}
